﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;

namespace Common
{
    public static class StringHelper
    {
        private static Regex RegTelePhone = new Regex(@"^\(?0\d{2,3}\)?[- ]?\d{7,8}|^0\d{2,3}[- ]?\d{7,8}|^1\d{10}|\(?\+?\d{2,3}\)?[- ]?0\d{2,3}[- ]?\d{7,8}|^\d{7,8}");
        private static Regex RegMobilePhone = new Regex(@"^1[3|4|5|7|8][0-9]\d{8}$");
        private static Regex RegURL = new Regex(@"(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*");
        private static Regex RegQQ = new Regex("^[1-9]*[1-9][0-9]*$");
        private static Regex RegNumber = new Regex("^[0-9]+$");
        private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
        private static Regex RegDecimal = new Regex("^[0-9]+(.[0-9]{1,2})?$"); //new Regex("^[0-9]+[.]?[0-9]+$");
        private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
        private static Regex RegEmail = new Regex(@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");//w 英文字母或数字的字符串，和 [a-zA-Z0-9] 语法一样 
        private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]");
        /// <summary>
        /// 时间转换成Unix时间戳
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string ConvertDateTimeToStamp(string dt)
        {
            try
            {
                DateTime datetime = Convert.ToDateTime(dt);
                return ((datetime.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
            }
            catch (Exception ex)
            {
                return null;
            } 
        }
        /// <summary>
        /// 时间戳 转换为时间
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <returns></returns>
        public static string ConvertStampToDateTime(string timeStamp)
        {
            if (timeStamp.Length == 0)
            {
                return "";
            }
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow).ToString("yyyy-MM-dd HH:mm:ss ");
        }

        public static bool EditTxt(string txtPath, int lineNum, string content)
        {
            //try
            //{
            //    string[] str = File.ReadAllLines(txtPath);
            //    str[lineNum] = content;
            //    File.WriteAllLines(txtPath, str);
            //    return true;
            //}
            //catch (Exception ex)
            //{
            //    return false;
            //    throw;
            //}
            string[] str = File.ReadAllLines(txtPath);
            str[lineNum] = content;
            File.WriteAllLines(txtPath, str);
            return true;
        }
        /// <summary> 
        /// DES加密 
        /// </summary> 
        /// <param name="encryptString"></param> 
        /// <returns></returns> 
        public static string DesEncrypt(string strEncryptString)
        {
            //strEncryptString = "account=huiyuan01&amount=1.00&bankkey=WEIXIN&banktype=2&merchantno=12345678&orderno=222111333789&87654321";
            StringBuilder strRetValue = new StringBuilder();
            string key = "12345678";
            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
                byte[] keyIV = keyBytes;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

                provider.Mode = CipherMode.CBC;//兼容其他语言的Des加密算法  
                provider.Padding = PaddingMode.PKCS7;//自动补0

                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //不使用base64编码
                //return Convert.ToBase64String(mStream.ToArray()); 

                //组织成16进制字符串            
                foreach (byte b in mStream.ToArray())
                {
                    strRetValue.AppendFormat("{0:X2}", b);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return strRetValue.ToString();
        }

        /// <summary> 
        /// DES解密 
        /// </summary> 
        /// <param name="decryptString"></param> 
        /// <returns></returns>         
        public static string DesDecrypt(string strDecryptString)
        {
            string strRetValue = "";

            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes("12345678".Substring(0, 8));
                byte[] keyIV = keyBytes;

                //不使用base64解码
                //byte[] inputByteArray = Convert.FromBase64String(decryptString);

                //16进制转换为byte字节
                byte[] inputByteArray = new byte[strDecryptString.Length / 2];
                for (int x = 0; x < strDecryptString.Length / 2; x++)
                {
                    int i = (Convert.ToInt32(strDecryptString.Substring(x * 2, 2), 16));
                    inputByteArray[x] = (byte)i;
                }

                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();

                provider.Mode = CipherMode.CBC;//兼容其他语言的Des加密算法  
                provider.Padding = PaddingMode.PKCS7;//自动补0  

                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();

                //需要去掉结尾的null字符
                //strRetValue = Encoding.UTF8.GetString(mStream.ToArray());
                strRetValue = Encoding.UTF8.GetString(mStream.ToArray()).TrimEnd('\0');
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return strRetValue;
        }
        /// <summary>
        /// 客户端终端类型
        /// </summary>
        /// <returns>1:终端为android手机 2：终端为苹果的ipad、iphone、ipod</returns>
        public static int GetTerminalType(string userAgent)
        {
            if (userAgent.Contains("android"))
            {
                return 1;
            }
            else if (userAgent.Contains("ipad") || userAgent.Contains("iphone"))
            {
                return 2;
            }
            return 0;
        }
        /// <summary>
        /// 随机生成字符串
        /// </summary>
        /// <returns></returns>
        public static string GetRandom(int strLength)
        {
            string strSep = ",";
            char[] chrSep = strSep.ToCharArray();
            string strChar = "1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
            string[] aryChar = strChar.Split(chrSep, strChar.Length);
            string strRandom = string.Empty;
            Random Rnd = new Random();

            //生成随机字符串
            for (int i = 0; i < strLength; i++)
            {
                strRandom += aryChar[Rnd.Next(aryChar.Length)];
            }
            return strRandom;

        }
        #region 数字字符串检查

        /// <summary>
        /// 匹配IP地址
        /// </summary>
        /// <param name="inputdata"></param>
        /// <returns></returns>
        public static bool IsIP(this string inputdata)
        {
            IPAddress ipadress;
            return IPAddress.TryParse(inputdata, out ipadress);
        }

        /// <summary>
        /// 匹配手机号码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsMobilePhone(this string inputData)
        {
            Match m = RegMobilePhone.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 匹配座机号码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsTelePhone(this string inputData)
        {
            Match m = RegTelePhone.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 匹配QQ号码
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsQQ(this string inputData)
        {
            Match m = RegQQ.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumber(this string inputData)
        {
            Match m = RegNumber.Match(inputData);
            return m.Success;
        }

        /// <summary>
        /// 是否数字字符串 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsNumberSign(this string inputData)
        {
            Match m = RegNumberSign.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimal(this string inputData)
        {
            Match m = RegDecimal.Match(inputData);
            return m.Success;
        }
        /// <summary>
        /// 是否是浮点数 可带正负号
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsDecimalSign(this string inputData)
        {
            Match m = RegDecimalSign.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 中文检测

        /// <summary>
        /// 检测是否有中文字符
        /// </summary>
        /// <param name="inputData"></param>
        /// <returns></returns>
        public static bool IsHasCHZN(this string inputData)
        {
            Match m = RegCHZN.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 邮件地址
        /// <summary>
        /// 是否是合法的邮箱地址
        /// </summary>
        /// <param name="inputData">输入字符串</param>
        /// <returns></returns>
        public static bool IsEmail(this string inputData)
        {
            Match m = RegEmail.Match(inputData);
            return m.Success;
        }

        #endregion

        #region 日期格式判断
        /// <summary>
        /// 日期格式字符串判断
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsDateTime(this string str)
        {
            try
            {
                if (!string.IsNullOrEmpty(str))
                {
                    DateTime.Parse(str);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region 检测URL地址
        /// <summary>
        /// 判断URL格式是否正确
        /// 日期：2013年10月8日21:57:31
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool IsURL(this string url)
        {
            Match match = RegURL.Match(url.Trim());
            if (match.Success)
            {
                return true;
            }
            return false;

        }
        #endregion

        #region 其他

        /// <summary>
        /// 将字符串根据分割符号转成字符串数组
        /// 日期：2013年10月14日20:22:20
        /// </summary>
        /// <param name="strList"></param>
        /// <param name="separatorIn"></param>
        /// <returns></returns>
        public static string[] Split(this string strList, char separatorIn)
        {
            char[] separator = { separatorIn };
            return strList.Split(separator);
        }

        public static string DelHTML(this string Htmlstring, int _int)//将?HTML去¨￡¤除y
        {
            Htmlstring = DeleteHtml(Htmlstring);
            Htmlstring = GetLenStr(Htmlstring, _int);
            return Htmlstring;

        }

        /// <summary>
        /// 完全去除Html标签
        /// 日期：2015年3月29日 10:55:43
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string DeleteHtml(this string Htmlstring)
        {
            #region
            //删|?除y脚?本à? 
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<STYLE[^>]*?>.*?</STYLE>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<style[^>]*?>.*?</style>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<style>([^>]*)</style>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            //删|?除yHTML
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"-->", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<!--.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<A>.*</A>", "");
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<[a-zA-Z]*=\.[a-zA-Z]*\?[a-zA-Z]+=\d&\w=%[a-zA-Z]*|[A-Z0-9]", "");
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(amp|#38);", "&", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(lt|#60);", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(gt|#62);", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&#(\d+);", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Htmlstring.Replace("\n", "");
            Htmlstring = Htmlstring.Replace(" ", "");
            Htmlstring = Htmlstring.Replace("  ", "");
            //Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring);
            #endregion
            return Htmlstring;
        }
        /// <summary>
        /// 去除指定的html标签
        /// 日期：2015年3月29日 10:55:43
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public static string DeleteHtmlforzdy(this string Htmlstring, string htmlstr)
        {
            #region
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<" + htmlstr + "[^>]*?>.*?</" + htmlstr + ">", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<" + htmlstr + "[^>]*?/>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            #endregion
            return Htmlstring;
        }

        public static string GetLenStr(this string str, int length)
        {
            if (str != "")
            {
                int i = 0, j = 0;
                foreach (char chr in str)
                {
                    if ((int)chr > 127)
                    {
                        i += 2;
                    }
                    else
                    {
                        i++;
                    }
                    if (i > length)
                    {
                        str = str.Substring(0, j) + "...";
                        break;
                    }
                    j++;
                }

            }
            return str;

        }

        public static string DelHTML(this string Htmlstring)
        {
            Htmlstring = DeleteHtml(Htmlstring);
            return Htmlstring;
        }

        public static string DeleteHTML(string Htmlstring)
        {
            string temp = DeleteHtml(Htmlstring);
            return temp;
        }


        /// <summary>
        /// 检查字符串最大长度，返回指定长度的串
        /// </summary>
        /// <param name="sqlInput">输入字符串</param>
        /// <param name="maxLength">最大长度</param>
        /// <returns></returns>			
        public static string SqlText(this string sqlInput, int maxLength)
        {
            if (sqlInput != null && sqlInput != string.Empty)
            {
                if (sqlInput.Length > maxLength)//按最大长度截取字符串
                    sqlInput = sqlInput.Substring(0, maxLength);
            }
            return sqlInput;
        }

        //字符串清理
        public static string InputText(this string inputString, int maxLength)
        {
            StringBuilder retVal = new StringBuilder();

            // 检查是否为空
            if ((inputString != null) && (inputString != String.Empty))
            {
                //检查长度
                if (inputString.Length > maxLength)
                    inputString = inputString.Substring(0, maxLength);

                //替换危险字符
                for (int i = 0; i < inputString.Length; i++)
                {
                    switch (inputString[i])
                    {
                        case '"':
                            retVal.Append("&quot;");
                            break;
                        case '<':
                            retVal.Append("&lt;");
                            break;
                        case '>':
                            retVal.Append("&gt;");
                            break;
                        default:
                            retVal.Append(inputString[i]);
                            break;
                    }
                }
                retVal.Replace("'", " ");// 替换单引号
            }
            return retVal.ToString().Trim();

        }
        /// <summary>
        /// 转换成 HTML code
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Encode(this string str)
        {
            str = str.Replace("&", "&amp;");
            str = str.Replace("'", "''");
            str = str.Replace("\"", "&quot;");
            str = str.Replace(" ", "&nbsp;");
            str = str.Replace("<", "&lt;");
            str = str.Replace(">", "&gt;");
            str = str.Replace("\n", "<br>");
            return str;
        }
        /// <summary>
        ///解析html成 普通文本
        /// </summary>
        /// <param name="str">string</param>
        /// <returns>string</returns>
        public static string Decode(this string str)
        {
            str = str.Replace("<br>", "\n");
            str = str.Replace("&gt;", ">");
            str = str.Replace("&lt;", "<");
            str = str.Replace("&nbsp;", " ");
            str = str.Replace("&quot;", "\"");
            return str;
        }

        public static string SqlTextClear(this string sqlText)
        {
            if (sqlText == null)
            {
                return null;
            }
            if (sqlText == "")
            {
                return "";
            }
            sqlText = sqlText.Replace(",", "");//去除,
            sqlText = sqlText.Replace("<", "");//去除<
            sqlText = sqlText.Replace(">", "");//去除>
            sqlText = sqlText.Replace("--", "");//去除--
            sqlText = sqlText.Replace("'", "");//去除'
            sqlText = sqlText.Replace("\"", "");//去除"
            sqlText = sqlText.Replace("=", "");//去除=
            sqlText = sqlText.Replace("%", "");//去除%
            sqlText = sqlText.Replace(" ", "");//去除空格
            return sqlText;
        }
        #endregion

        #region 是否由特定字符组成
        public static bool IsContainSameChar(this string strInput)
        {
            string charInput = string.Empty;
            if (!string.IsNullOrEmpty(strInput))
            {
                charInput = strInput.Substring(0, 1);
            }
            return IsContainSameChar(strInput, charInput, strInput.Length);
        }

        public static bool IsContainSameChar(string strInput, string charInput, int lenInput)
        {
            if (string.IsNullOrEmpty(charInput))
            {
                return false;
            }
            else
            {
                Regex RegNumber = new Regex(string.Format("^([{0}])+$", charInput));
                //Regex RegNumber = new Regex(string.Format("^([{0}]{{1}})+$", charInput,lenInput));
                Match m = RegNumber.Match(strInput);
                return m.Success;
            }
        }
        #endregion

        #region 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查
        /// <summary>
        /// 检查输入的参数是不是某些定义好的特殊字符：这个方法目前用于密码输入的安全检查
        /// </summary>
        public static bool IsContainSpecChar(this string strInput)
        {
            string[] list = new string[] { "123456", "654321" };
            bool result = new bool();
            for (int i = 0; i < list.Length; i++)
            {
                if (strInput == list[i])
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 检测是否含有非法字符
        /// </summary>
        /// <param name="badword"></param>
        /// <returns></returns>
        public static bool CheckSafeWord(this string badword)
        {
            string[] bw = new string[15];
            bw[0] = "'";
            bw[1] = "\"";
            bw[2] = ";";
            bw[3] = "--";
            bw[4] = ",";
            bw[5] = "!";
            bw[6] = "~";
            bw[7] = "@";
            bw[8] = "#";
            bw[9] = "$";
            bw[10] = "%";
            bw[11] = "^";
            bw[12] = "&";
            bw[13] = "  ";
            bw[14] = "_";

            bool isok = false;
            foreach (string str in bw)
            {
                if (badword.IndexOf(str) > -1)
                {
                    isok = true;
                    return isok;
                }
            }
            return isok;
        }

        /// <summary>
        /// 根据日期产生数据编码
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreateGUIDByDateTime()
        {

            return DateTime.Now.ToString("yyMMddHHmmss");
        }

        /// <summary>
        /// 根据日期产生数据编码 ToFileTimeUtc
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreateGUIDByDateTimeFromFileTimeUtc()
        {

            return DateTime.Now.ToFileTimeUtc().ToString();
        }

        public static string CreateGUID(string preFix)
        {
            string guid = preFix.ToUpper();
            string randomCode = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            int randomCodeLength = randomCode.Length;
            string datetime = DateTime.Now.ToString("yy{0}{1}MM{2}{3}dd{4}{5}HH{6}{7}mm{8}{9}ss");
            Random random = new Random();
            guid = guid + string.Format(datetime,
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1),
                randomCode.Substring(random.Next(randomCodeLength), 1)
                );
            return guid;
        }

        /// <summary>
        /// 生成数据库中数据ID
        /// 于挺
        /// 2013年1月7日 10:40:32
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string CreateGUID()
        {
            return Guid.NewGuid().ToString("N").Trim();
        }

        /// <summary>
        /// 分析用户请求是否正常
        /// </summary>
        /// <param name="Str">传入用户提交数据</param>
        /// <returns>返回是否含有SQL注入式攻击代码</returns>
        public static bool ProcessSqlStr(this string Str)
        {
            bool ReturnValue = true;
            try
            {
                if (Str != "" && Str != null)
                {
                    string SqlStr = "";
                    if (SqlStr == "" || SqlStr == null)
                    {
                        SqlStr = "'|and|exec|insert|select|delete|update|count|mid|master|truncate|char|declare";
                    }
                    string[] anySqlStr = SqlStr.Split('|');
                    foreach (string ss in anySqlStr)
                    {
                        if (Str.IndexOf(ss) >= 0)
                        {
                            ReturnValue = false;
                        }
                    }
                }
            }
            catch
            {
                ReturnValue = false;
            }
            return ReturnValue;
        }

        /// <summary>
        /// 把字符串“1,2,3”变成“'1','2','3'”
        /// </summary>
        /// <param name="String"></param>
        /// <returns></returns>
        public static string GetSqlString(string String)
        {
            if (String.Length > 0)
            {
                String = String.Replace(",", "','");
                String = "'" + String + "'";
            }
            return String;
        }

        /// <summary>
        /// 去除Html标签
        /// 日期：2013年10月2日18:15:51
        /// </summary>
        /// <param name="str"></param>
        /// <returns>string</returns>
        public static string ReplaceHTML(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return str.Replace("<", "&lt;").Replace(">", "&gt;").Replace("''", "&quot;").Replace(";", "；");
            }
            return "";
        }

        /// <summary>
        /// 将全角字符转换成半角字符
        /// 日期：2014年12月3日 18:35:34
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static string ConvertToDBC(this string inputStr)
        {
            char[] c = inputStr.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new String(c);
        }

        /// <summary>
        /// 将半角字符转换成全角字符
        /// 日期：2014年12月3日 18:37:21
        /// </summary>
        /// <param name="inputStr"></param>
        /// <returns></returns>
        public static string ConvertToSBC(this string inputStr)
        {
            // 半角转全角：
            char[] c = inputStr.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                    c[i] = (char)(c[i] + 65248);
            }
            return new String(c);
        }

        /// <summary>
        /// 得到随机哈希加密字符串
        /// </summary>
        /// <returns></returns>
        public static string GetSecurity()
        {
            string Security = HashEncoding(GetRandomValue());
            return Security;
        }

        /// <summary>
        /// 得到一个随机数值
        /// </summary>
        /// <returns></returns>
        public static string GetRandomValue()
        {
            Random Seed = new Random();
            string RandomVaule = Seed.Next(1, int.MaxValue).ToString().Trim();
            return RandomVaule;
        }

        /// <summary>
        /// 哈希加密一个字符串
        /// </summary>
        /// <param name="Security"></param>
        /// <returns></returns>
        private static string HashEncoding(string Security)
        {
            byte[] Value;
            UnicodeEncoding Code = new UnicodeEncoding();
            byte[] Message = Code.GetBytes(Security);
            SHA512Managed Arithmetic = new SHA512Managed();
            Value = Arithmetic.ComputeHash(Message);
            Security = "";
            foreach (byte o in Value)
            {
                Security += (int)o + "O";
            }
            return Security;
        }

        ///   <summary> 
        ///   MD5加密 
        ///   </summary> 
        ///   <param   name= "strString "> </param> 
        ///   <param   name= "strKey "> </param> 
        ///   <param   name= "encoding "> </param> 
        ///   <returns> </returns>  
        public static string MD5(this string str)
        {
            byte[] bValue;
            byte[] bHash;
            string result = null;
            MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();

            bValue = System.Text.Encoding.UTF8.GetBytes(str);

            bHash = MD5.ComputeHash(bValue);

            MD5.Clear();

            for (int i = 0; i < bHash.Length; i++)
            {
                if (bHash[i].ToString("x").Length == 1)
                {
                    //如果返回来是07这样的值，0会被省掉，所以强制加了一个0   
                    result += "0" + bHash[i].ToString("x");
                }
                else
                {
                    result += bHash[i].ToString("x");
                }

            }
            return result.ToLower();
        }  
        /// <summary>
        /// 根据URL获取域名
        /// 日期：2015年3月1日 14:05:26
        /// </summary>
        /// <param name="Url"></param>
        /// <returns>string</returns>
        public static string ExtractDomainNameFromURL(this string Url)
        {
            if (!Url.Contains("://"))
                Url = "http://" + Url;

            return new Uri(Url).Host;
        }


        /// <summary>
        /// 产生指定长度的短信验证码
        /// 日期：2015年4月21日18:41:03
        /// </summary>
        /// <param name="length"></param>
        /// <returns>string</returns>
        public static string CreateSMSValidateCode(int length)
        {
            string randomVaule = "";
            Random Seed = new Random();
            for (int i = 1; i <= length; i++)
            {
                randomVaule = randomVaule + Seed.Next(0, 9).ToString().Trim();
            }

            return randomVaule;
        }


        /// <summary>
        /// 将unicode字符转为中文
        /// </summary>
        /// <param name="str_input"></param>
        /// <returns></returns>
        public static string UnicodeToChar(this string str_input)
        {
            string r = "";
            MatchCollection mc = Regex.Matches(str_input, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
            byte[] bts = new byte[2];
            foreach (Match m in mc)
            {
                bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
                bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
                r += Encoding.Unicode.GetString(bts);
            }
            return r;

        }
        /// <summary>
        /// 获取客户端的 IP 信息
        /// </summary>
        /// <returns></returns>
        public static string GetUserIP()
        {
            if (HttpContext.Current == null)
            {
                return string.Empty;
            }
            string ipval = string.Empty;
            ipval = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            switch (ipval)
            {
                case null:
                case "":
                    ipval = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                    break;
            }
            if ((ipval == null) || (ipval == string.Empty))
            {
                ipval = HttpContext.Current.Request.UserHostAddress;
            }
            if (!(((ipval != null) && (ipval != string.Empty))))
            {
                return "0.0.0.0";
            }
            if (ipval == "::1")
            {//本地测试
                return "125.69.46.38";//电信
            }
            return ipval;
        }

    }
}
